package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserTestService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统设置-用户管理
 */

@RestController
@RequestMapping("/userTest")
public class UserTestController {
    //调用service
    @Reference
    private UserTestService userTestService;

    /**
     * 分页查所有用户
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = userTestService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    /**
     * 新增用户,角色列表回显
     */
    @RequestMapping("/findRoleAll")
    public Result findRoleAll() {
        List<Role> rolesList = userTestService.findRoleAll();
        if (rolesList != null && rolesList.size() > 0) {
            return new Result(true, "角色回显成功", rolesList);
        }
        return new Result(false, "角色回显失败");
    }

    /**
     * 新增用户
     */
    @RequestMapping("/add")
    public Result add(Integer[] roleIds, @RequestBody User user) {
        try {
            userTestService.add(user, roleIds);
        } catch (Exception e) {
            return new Result(false, "添加角色失败");
        }
        return new Result(true, "添加角色成功");
    }

    /**
     * 编辑用户-根据用户id回显用户信息
     */
    @RequestMapping("/findById")
    public Result findById(Integer userId) {
        User user = userTestService.findById(userId);
        if (user != null) {
            Result result = new Result(true, "用户回显信息成功", user);
            return result;
        }
        return new Result(false, "用户回显信息失败");
    }

    /**
     * 编辑用户-根据用户id查询角色ids
     */
    @RequestMapping("/findRoleIdsByUserId")
    public List<Integer> findRoleIdsByUserId(@RequestParam("userId") Integer Id) {
        List<Integer> list = userTestService.findRoleIdsByUserId(Id);
        return list;
    }

    /**
     * 编辑用户-保存用户信息
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user,Integer[] roleIds){
        try {
            userTestService.edit(user,roleIds);
        } catch (Exception e) {
           return new Result(false,"编辑数据保存失败");
        }
        return new Result(true,"编辑数据保存成功");
    }
    /**
     * 删除用户
     */
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        try {
            userTestService.deleteById(id);
            return new Result(true,"删除用户成功");
        }catch(RuntimeException e){
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"系统繁忙,删除用户失败");
        }
    }
}